
#import "BestLightViewController.h"
#import "HelpViewController.h"
#import "UIViewController+ModalWithNavBar.h"
#import "UINavigationItem+InfoButton.h"
#import "RoomGalleryViewController.h"
#import "GalleryRoom.h"
#import "DocumentViewController.h"
#import "BestLightCell.h"


@interface BestLightViewController () <BestLightCellDelegate>

@property (strong, nonatomic) NSArray *roomData;

@end

@implementation BestLightViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self loadRoomData];
    
    [self.navigationItem addInfoButtonWithTarget:self action:@selector(showInfo)];
}

-(void)loadRoomData {
    NSError *error = nil;
    NSString *path = [[NSBundle mainBundle] pathForResource:@"roomdata" ofType:@"json"];
    NSData *jsonData = [NSData dataWithContentsOfFile:path];
    NSArray *jsonRooms = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error];
    
    NSMutableArray *roomData = [NSMutableArray arrayWithCapacity:jsonRooms.count];
    for (NSDictionary *jsonRoom in jsonRooms) {
        [roomData addObject:[GalleryRoom instanceFromDictionary:jsonRoom]];
    }
    self.roomData = roomData;
}

- (void)showInfo
{
    HelpViewController *helpVC = [[HelpViewController alloc] initWithNibName:nil bundle:nil];
    helpVC.documentFile = _(@"Lighting");
    helpVC.title = _(@"Information");
    [self presentModalWithNavBar:helpVC];
}

-(NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

#pragma mark - Table view stuff
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.roomData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *kCellIdentifier = @"BestLightCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:17];
    }
    
    GalleryRoom *room = self.roomData[indexPath.row];
    cell.textLabel.text = room.name;

    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    [self bestLightCellPressedWithTag:indexPath.row];
}

#pragma mark - Best light cell delegate
- (void)bestLightCellPressedWithTag:(NSInteger)tag
{
    GalleryRoom *room = self.roomData[tag];
    
    if (voiceoverEnabled) {
        DocumentViewController *documentVC = [[DocumentViewController alloc] init];
        [documentVC showDocumentWithName:room.documentName];
        documentVC.backButtonTitle = self.navigationItem.title;
        documentVC.title = room.name;
        [self.navigationController pushViewController:documentVC animated:YES];
    } else {
        RoomGalleryViewController *roomsVC = [[RoomGalleryViewController alloc] initWithRooms:self.roomData showRoomNumber:tag];
        [self presentViewController:roomsVC animated:NO completion:nil];
    }
}

@end
